//
//  ChallengeModel.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/1.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "ChallengeModel.h"

@implementation ChallengeModel
- (ChallengeModel *)initWithChallengeModel:(nonnull UIView *)tableView{
    self.btnStart = [tableView viewWithTag:10];
    [self btnToEnabled];
    self.tableViewMain = (UITableView *)tableView;
    self.btnDistance = [tableView viewWithTag:6];
    [self.btnDistance addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    self.btnTime = [tableView viewWithTag:7];
    [self.btnTime addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    self.btnHeat = [tableView viewWithTag:8];
    [self.btnHeat addTarget:self action:@selector(selectType:) forControlEvents:UIControlEventTouchUpInside];
    self.labNumber = [tableView viewWithTag:11];
    self.labNumber.hidden = YES;
    self.setType = SetTypeDefault;
    return self;
}

- (void)initSetView:(nonnull UIView *)tableView{
    self.btnUnselect = [tableView viewWithTag:2];
    self.btnSelect = [tableView viewWithTag:3];
    self.labTiele = [tableView viewWithTag:4];
    self.labUnit = [tableView viewWithTag:5];
    [self.btnUnselect addTarget:self action:@selector(selectNumber:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnSelect addTarget:self action:@selector(selectNumber:) forControlEvents:UIControlEventTouchUpInside];
    if (self.setType == SetTypeDistance) {
        self.labTiele.text = @"运动目标：距离";
        self.labUnit.text =  @"公里";
    }else if (self.setType == SetTypeHeat){
        self.labTiele.text = @"运动目标：卡路里";
        self.labUnit.text =  @"千卡";
    }else if (self.setType == SetTypeTime){
        self.labTiele.text = @"运动目标：时间";
        self.labUnit.text =  @"分钟";
    }
}

- (void)setSetType:(SetType)setType{
    if (setType == SetTypeDistance) {
        [self.btnDistance setBackgroundImage:[UIImage imageNamed:@"icon_distance"] forState:UIControlStateNormal];
        [self.btnTime setBackgroundImage:[UIImage imageNamed:@"icon_untime"] forState:UIControlStateNormal];
        [self.btnHeat setBackgroundImage:[UIImage imageNamed:@"icon_unheat"] forState:UIControlStateNormal];
        _setType = SetTypeDistance;
    }else if (setType == SetTypeTime){
        [self.btnDistance setBackgroundImage:[UIImage imageNamed:@"icon_undistance"] forState:UIControlStateNormal];
        [self.btnTime setBackgroundImage:[UIImage imageNamed:@"icon_time"] forState:UIControlStateNormal];
        [self.btnHeat setBackgroundImage:[UIImage imageNamed:@"icon_unheat"] forState:UIControlStateNormal];
        _setType = SetTypeTime;
    }else if (setType == SetTypeHeat){
        [self.btnDistance setBackgroundImage:[UIImage imageNamed:@"icon_undistance"] forState:UIControlStateNormal];
        [self.btnTime setBackgroundImage:[UIImage imageNamed:@"icon_untime"] forState:UIControlStateNormal];
        [self.btnHeat setBackgroundImage:[UIImage imageNamed:@"icon_heat"] forState:UIControlStateNormal];
        _setType = SetTypeHeat;
    }else{
        [self.btnDistance setBackgroundImage:[UIImage imageNamed:@"icon_undistance"] forState:UIControlStateNormal];
        [self.btnTime setBackgroundImage:[UIImage imageNamed:@"icon_untime"] forState:UIControlStateNormal];
        [self.btnHeat setBackgroundImage:[UIImage imageNamed:@"icon_unheat"] forState:UIControlStateNormal];
        _setType = SetTypeDefault;
    }
}

- (void)selectType:(id)sent{
    UIButton *btnSelect = (UIButton *)sent;
    if (btnSelect.tag == 6) {
        self.setType = SetTypeDistance;
    }else if (btnSelect.tag == 7){
        self.setType = SetTypeTime;
    }else{
        self.setType = SetTypeHeat;
    }
    self.challengeYourselfType = ChallengeYourselfTypeSet;
    [self.tableViewMain reloadData];
}

- (void)selectNumber:(id)sent{
    UIButton *btnSelect = (UIButton *)sent;
    if (btnSelect.tag == 2) {
        self.select = NO;
    }else if (btnSelect.tag == 3){
        self.select = YES;
    }
    if (self.select == YES) {
        self.labNumber.hidden = NO;
        if (self.setType == SetTypeDistance) {
            self.strDistance = [NSString stringWithFormat:@"%@",self.number];
            self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@公里",self.strDistance];
        }else if (self.setType == SetTypeTime){
            self.strTime = [NSString stringWithFormat:@"%@",self.number];
            self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@分钟",self.strTime];
        }else{
            self.strHeat = [NSString stringWithFormat:@"%@",self.number];
            self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@千卡",self.strHeat];
        }
    }else{
        if (self.setType == SetTypeDistance) {
            if (self.strDistance.length != 0) {
                self.labNumber.hidden = NO;
                self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@公里",self.strDistance];
            }else{
                self.labNumber.hidden = YES;
            }
        }else if (self.setType == SetTypeTime){
            if (self.strTime.length != 0) {
                self.labNumber.hidden = NO;
                self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@分钟",self.strTime];
            }else{
                self.labNumber.hidden = YES;
            }
        }else{
            if (self.strHeat.length != 0) {
                self.labNumber.hidden = NO;
                self.labNumber.text = [NSString stringWithFormat:@"您设置的运动目标为%@千卡",self.strHeat];
            }else{
                self.labNumber.hidden = YES;
            }
        }
    }
    if (self.strDistance.length != 0 || self.strTime.length != 0 || self.strHeat.length != 0) {
        self.btnStart.enabled = YES;
        [self.btnStart setBackgroundImage:[UIImage imageNamed:@"icon_pay"] forState:UIControlStateNormal];
    }
    self.challengeYourselfType = ChallengeYourselfTypeHome;
    [self.tableViewMain reloadData];
}

- (void)btnToEnabled{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1.8];
    [UIView setAnimationDelegate:self];
    self.btnStart.enabled = NO;
    [UIView commitAnimations];
}
@end
